#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "dragframe.h"
#include "history.h"
#include "options.h"
#include "dialogvol.h"
#include "aboutdialog.h"
#include "ledthread.h"
#include "parameter.h"

#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QSqlQueryModel>
#include <QModbusDataUnit>
#include <QSerialPort>
#include <QtMultimedia>
#include <QtMultimedia>
#include <QDir>
#include <QNetworkAccessManager>
#include <QNetworkReply>

QT_BEGIN_NAMESPACE
namespace Ui
{
class MainWindow;
}
class QModbusClient;
class QModbusReply;
QT_END_NAMESPACE


class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget* parent = nullptr);
    ~MainWindow();

    QString iniFile;
    QMediaPlayer* player;
    QMediaPlaylist* playlist;
    QList<QFileInfo>* fileInfo;
    QDir* dir;
    DialogVol* dlgvol = nullptr;
    bool dbConnection();
    bool modbusConnection();
    bool serialConnection();
    void serialDisConnection();


private slots:
    void onModbusStateChanged(int state);
    void readReadyUnitA(); // 通道A单位获取槽函数
    void readReadyUnitB(); // 通道A单位获取槽函数
    void readReady(); //读MSA卡的浓度数值槽函数
    void readCardValue();
    void playAlarmSound(int fileIndex);
    void playWarningSound();
    void testWarningSound(int index);
    void readResponse();
    void httpPost(int id, QString json);

    void on_actionexit_triggered();
    void on_actionConnect_triggered();
    void on_actionDisconnect_triggered();
    void on_actionhistory_triggered();
    void on_actionOption_triggered();
    void on_actionreboot_triggered();
    void on_actionshutdown_triggered();
    void on_actionvolume_triggered();
    void on_actiononbord_triggered();
    void on_actionslaveserial_triggered();
    void on_actioncloseSlave_triggered();
    void on_actionabout_triggered();
    void on_actiontestalarmsound_triggered();
    void on_actionreset_triggered();



private:
    Ui::MainWindow* ui;
    QModbusClient* modbusDevice = nullptr;
    QSerialPort* serialPort = nullptr;

    DragFrame* w[MAX_CARD_COUNT];
    QHash<int, DragFrame*> ws;   //modbus地址跟DragFrame关联，通过modbus地址访问DragFrame
    History* historyWidget = nullptr;
    options* opt = nullptr;

    QSqlDatabase db;
    QLabel* ComStatic, *ledTx, *ledRx;
    QTimer* ledRxOff, *ledTxOff, *readCardTimer;
    int mp3count, sysVolume, alarmVolume, alarmFileIndex;
    ledThread* ledRed, *ledLime;
    bool authorized; // 受权标志
    QNetworkAccessManager* m_http = nullptr;
    QString httpHost, httpUserName;
    int httpPort = 9090;
    bool updateTB;
    int historyRecodeSaveDays;

    void initFrom();
    void initMsaCard();
    void initThingsBoard();
    void setLedRxOn();
    void setLedTxOn();
    void readMsaUnitA(int cardIndex); // 读MSA卡的单位
    void readMsaUnitB(int cardIndex); // 读MSA卡的单位
    void readMsaData(int cardIndex); // 读MSA卡的浓度
    void deleteRecoder();// 删除历史记录
};
#endif // MAINWINDOW_H
